﻿using System;
using UnityEngine;

// Token: 0x020002E6 RID: 742
public class BGM_Control
{
	// Token: 0x060012CD RID: 4813 RVA: 0x000877E8 File Offset: 0x000859E8
	public BGM_Control(AudioSource source)
	{
		this.source = source;
		this.fadeVol = 0f;
		this.nextClip = null;
		this.nowCommand = BGM_Control.COMMAND.STOP;
		this.goalCommand = BGM_Control.COMMAND.STOP;
		this.fadeSpeed = 1f;
	}

	// Token: 0x170001BD RID: 445
	// (get) Token: 0x060012CE RID: 4814 RVA: 0x00087848 File Offset: 0x00085A48
	public bool IsPlaying
	{
		get
		{
			return this.nowCommand == BGM_Control.COMMAND.PLAY;
		}
	}

	// Token: 0x060012CF RID: 4815 RVA: 0x00087854 File Offset: 0x00085A54
	public void Update()
	{
		if ((this.nowCommand != this.goalCommand || this.nextClip != null) && this.fadeVol <= 0f)
		{
			if (this.nextClip != null)
			{
				this.ChangeClip(this.nextClip);
				this.nextClip = null;
			}
			if (this.goalCommand == BGM_Control.COMMAND.PLAY)
			{
				this.Play();
				this.nowCommand = this.goalCommand;
			}
			else if (this.goalCommand == BGM_Control.COMMAND.STOP)
			{
				this.Stop();
				this.nowCommand = this.goalCommand;
			}
			else if (this.goalCommand == BGM_Control.COMMAND.PAUSE)
			{
				this.Pause();
				this.nowCommand = this.goalCommand;
			}
		}
		this.UpdateVolume();
	}

	// Token: 0x060012D0 RID: 4816 RVA: 0x00087924 File Offset: 0x00085B24
	private void UpdateVolume()
	{
		float num = this.fadeVol;
		this.GoalVol(ref num);
		if (this.fadeVol != num)
		{
			if (this.fadeSpeed > 0f)
			{
				float value = num - this.fadeVol;
				float num2 = this.fadeSpeed * Time.deltaTime;
				this.fadeVol += Mathf.Clamp(value, -num2, num2);
			}
			else
			{
				this.fadeVol = num;
			}
		}
		this.source.volume = this.Volume(this.fadeVol);
	}

	// Token: 0x060012D1 RID: 4817 RVA: 0x000879AC File Offset: 0x00085BAC
	private void GoalVol(ref float goalVol)
	{
		if (this.nextClip != null)
		{
			goalVol = 0f;
		}
		else
		{
			goalVol = ((this.goalCommand != BGM_Control.COMMAND.PLAY) ? 0f : 1f);
		}
	}

	// Token: 0x060012D2 RID: 4818 RVA: 0x000879E8 File Offset: 0x00085BE8
	public void Play(float speed)
	{
		this.fadeSpeed = speed;
		this.goalCommand = BGM_Control.COMMAND.PLAY;
	}

	// Token: 0x060012D3 RID: 4819 RVA: 0x000879F8 File Offset: 0x00085BF8
	public void Pause(float speed)
	{
		this.fadeSpeed = speed;
		this.goalCommand = BGM_Control.COMMAND.PAUSE;
	}

	// Token: 0x060012D4 RID: 4820 RVA: 0x00087A08 File Offset: 0x00085C08
	public void Stop(float speed)
	{
		this.fadeSpeed = speed;
		this.goalCommand = BGM_Control.COMMAND.STOP;
	}

	// Token: 0x060012D5 RID: 4821 RVA: 0x00087A18 File Offset: 0x00085C18
	public void ChangeClip(AudioClip clip, float speed)
	{
		this.fadeSpeed = speed;
		this.nextClip = clip;
	}

	// Token: 0x060012D6 RID: 4822 RVA: 0x00087A28 File Offset: 0x00085C28
	private void Play()
	{
		if (this.source.clip == null)
		{
			GlobalData.Debug_LogWarning("在没有剪辑的情况下，我试着播放。");
		}
		this.source.Play();
	}

	// Token: 0x060012D7 RID: 4823 RVA: 0x00087A58 File Offset: 0x00085C58
	private void Pause()
	{
		this.source.Pause();
	}

	// Token: 0x060012D8 RID: 4824 RVA: 0x00087A68 File Offset: 0x00085C68
	private void Stop()
	{
		this.source.Stop();
	}

	// Token: 0x060012D9 RID: 4825 RVA: 0x00087A78 File Offset: 0x00085C78
	private void ChangeClip(AudioClip clip)
	{
		bool isPlaying = this.source.isPlaying;
		this.source.clip = clip;
		if (isPlaying)
		{
			this.Play();
		}
	}

	// Token: 0x060012DA RID: 4826 RVA: 0x00087AAC File Offset: 0x00085CAC
	private float Volume(float vol)
	{
		return ConfigData.VolumeBGM() * vol;
	}

	// Token: 0x040017C2 RID: 6082
	private AudioSource source;

	// Token: 0x040017C3 RID: 6083
	private float fadeVol;

	// Token: 0x040017C4 RID: 6084
	private AudioClip nextClip;

	// Token: 0x040017C5 RID: 6085
	private BGM_Control.COMMAND nowCommand = BGM_Control.COMMAND.STOP;

	// Token: 0x040017C6 RID: 6086
	private BGM_Control.COMMAND goalCommand = BGM_Control.COMMAND.STOP;

	// Token: 0x040017C7 RID: 6087
	private float fadeSpeed = 1f;

	// Token: 0x020002E7 RID: 743
	private enum COMMAND
	{
		// Token: 0x040017C9 RID: 6089
		NONE,
		// Token: 0x040017CA RID: 6090
		PLAY,
		// Token: 0x040017CB RID: 6091
		PAUSE,
		// Token: 0x040017CC RID: 6092
		STOP,
		// Token: 0x040017CD RID: 6093
		CHANGE
	}
}
